// TypeScript file
/**
 * 网络管理类
 */
class NetMgr extends egret.DisplayObject {

    private socket: egret.WebSocket = new egret.WebSocket();
    static net: NetMgr;
    private recv_buffer: egret.ByteArray = new egret.ByteArray();
    private send_buffer: egret.ByteArray = new egret.ByteArray();

    constructor() {
        super();

    }
    public static GetInstance(): NetMgr {
        if (this.net == null) {
            this.net = new NetMgr();

        }
        return this.net;
    }

    public StartSocket(serverip: string, port: number): void {
        if (this.socket.connected) return;
        this.socket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
        this.socket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
        this.socket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.IOError, this);
        this.socket.addEventListener(egret.Event.CLOSE, this.Close, this);
        this.socket.connect(serverip, port)


    }

    /**
     * 发送消息
     */
    public sendMsg(msg: SocketMsg): void {
        try {
            this.send_buffer.clear();
            this.send_buffer.endian = egret.Endian.BIG_ENDIAN;
            this.send_buffer.writeInt(msg.buff_msg.length);
            this.send_buffer.endian = egret.Endian.LITTLE_ENDIAN;
            this.send_buffer.writeBytes(msg.buff_msg);

            this.socket.writeBytes(this.send_buffer, 0, this.send_buffer.bytesAvailable);
            this.socket.flush();
        } catch (e) {
            console.error(e);
        }
    }

    public GetStatus(): boolean {
        return this.socket.connected;
    }



    onReceiveMessage(): void {
        
        this.socket.readBytes(this.recv_buffer, this.recv_buffer.position);

        if (this.recv_buffer.bytesAvailable > 4) {

            this.recv_buffer.endian = egret.Endian.BIG_ENDIAN;
            let msg_len = this.recv_buffer.readInt();
            this.recv_buffer.endian = egret.Endian.LITTLE_ENDIAN;

            if (this.recv_buffer.bytesAvailable >= msg_len) {

                let msg = new SocketMsg();
                msg.fill(this.recv_buffer, this.recv_buffer.position, msg_len);

                this.processReceive(msg);
            }
            else
            {
                console.log("msg len error");
            }

            this.recv_buffer.clear();
        }
    }

    private processReceive(msg: SocketMsg) {
        console.log("recv msg : " + msg.name);

        switch (msg.name) {
            case "AuthOk":
                break;

            case "LOGIN":
                console.log(msg.rint32());
                console.log(msg.rdouble());
                console.log(msg.rstring());
                break;
            case "SyncPlayerInfo":
                let player_info = msg.rstring();
                console.log(player_info);

                break;
            default:
                break;
        }
    }


    Close(): void {
        console.log("连接关闭")

    }

    onSocketOpen(): void {

        console.log("网络连接成功");

    }

    IOError(): void {

        console.log("网络连接断开")


    }

    public Emit<T>(controller: string, action: string, data: T): void {
        if (!this.socket.connected) {
            let protocol = new Protocol<T>();
            protocol.controller = controller;
            protocol.action = action;
            protocol.data = data;
            let json = JSON.stringify(protocol);
            this.socket.writeUTF(json);
        }

    }


}